#include "Image.h"

using namespace neiderra::rendering;

Image::Image()
{
	width_=height_=0;
	data_ = 0;
}

Image::Image(const unsigned width, const unsigned height,
			 nByte* data)
{
	width_ = width;
	height_ = height;
	data_ = data;
}

Image::Image(Image& img)
{
	width_ = img.getWidth();
	height_ = img.getHeight();

	const unsigned size = width_*height_*3;
	data_ = new nByte[size];	// 24-bit

	nByte* impdata = img.getData();
	for(unsigned i=0; i < size; i++) data_[i]=impdata[i];
}

Image::Image(unsigned width, unsigned height)
{
	width_ = width;
	height_ = height;
	const unsigned size = width*height*3;
	data_ = new nByte[size];

	for(unsigned i=0; i<size; i++) data_[i]=0;
}

Image& Image::operator=(const Image& img)
{
	if(&img!=this){
		Image(img);
	}
	return *this;
}

Image::~Image()
{
	delete data_;
}

unsigned Image::getWidth(){return width_;}

unsigned Image::getHeight(){return height_;}

nByte* Image::getData(){return data_;}

inline bool setPixel(unsigned x, unsigned y, nByte rgba[])
{
}

Image* Image::copy(int x, int y, int width, int height)
{
	if(x+width>width_) return 0;
	if(y+height<height_) return 0;

	Image* img = new Image(width, height);
	nByte* data = img->getData();

	for(int j=0;j<height_; j++)
	{
		const int pos = j*3*width_;
        const int rel_pos = (j+y)*3*width_;
			
        for(int i=0; i<width_;i++)
		{
			data[pos + i + 0] = data_[rel_pos+ i + x + 0];
			data[pos + i + 1] = data_[rel_pos+ i + x + 1];
			data[pos + i + 2] = data_[rel_pos+ i + x + 2];
		}
	}

	return img;
}

Image& Image::paste(int x, int y, Image* img)
{
	// ���� �������
	const unsigned imgW = img->getWidth();
	const unsigned imgH = img->getHeight();

	if(imgW +x > width_) return *this; 
	if(imgH +y > height_) return *this;

	nByte* data = img->getData();

	for(int j=0;j<imgH;j++)
	{
		const int pos = j*3*imgW;
        const int rel_pos = (j+y)*3*width_;

		for(int i=0;i<imgW;i++)
		{
			const int li = i*3;
			const int lxi = (i+x)*3;
            data_[rel_pos + lxi + 0] = data[pos+ li + 0];
			data_[rel_pos + lxi + 1] = data[pos+ li + 1];
			data_[rel_pos + lxi + 2] = data[pos+ li + 2];
		}
	}

	return *this;
}
